<#if invalid??>
<div class="row">
	<div class="col-md-12">
		<div class="alert alert-danger" role="alert">
			<strong>无法创建合同！</strong> 无效的主合同信息或者系统异常！
		</div>
	</div>
</div>
<#else>
<form class="form-create-agreement-contract" action="${request.contextPath}/sub-contract/create" method="post" enctype="multipart/form-data">
	<input type="hidden" name="parent.id" value="${parent.id}"> <input type="hidden" name="status" value="COMPLETED"> <input type="hidden" name="type" value="${type}"> <input type="hidden" name="price" value="0"> <input type="hidden" name="moneyType" value="TOTAL">

	<div class="row">
		<div class="col-md-12">
			<div class="form-group text-center">
				<h4>${parent.name}（${type.value}）</h4>
				<small>[${parent.no}]</small>
			</div>
		</div>
	</div>

	<fieldset>
		<legend class="text-primary">基本信息</legend>
		<div class="row">
			<div class="col-md-12">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">
								<span class="not-null-star">*</span> 协议名称
							</div>
						</div>
						<input type="text" class="form-control not-empty" name="name" value="${parent.name}XXX${type.value}" placeholder="最多100个汉字">
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">
								<span class="not-null-star">*</span> 协议编号
							</div>
						</div>
						<input type="text" class="form-control not-empty" name="no" value="${parent.no}-XXX" placeholder="最多50个字符">
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">
								<span class="not-null-star">*</span> 签订日期
							</div>
						</div>
						<input type="text" class="form-control not-empty datetimepicker" name="signDate" data-foot="false" data-format="yyyy-MM-dd" value="${.now?string('yyyy-MM-dd')}" readonly="readonly">
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">
								<span class="not-null-star">*</span> 建设单位
							</div>
						</div>
						<input type="text" class="form-control not-empty" name="companyA" value="${parent.companyA}" readonly="readonly">
					</div>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">
								<span class="not-null-star">*</span> 承建单位
							</div>
						</div>
						<input type="text" class="form-control not-empty" name="companyB" value="${parent.companyB}" readonly="readonly">
					</div>
				</div>
			</div>
			<div class="col-md-12">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">备注</div>
						</div>
						<textarea rows="3" class="form-control" name="remark" placeholder="最多1000个汉字"></textarea>
					</div>
				</div>
			</div>
		</div>
	</fieldset>

	<fieldset>
		<legend class="text-success">相关文件</legend>
		<div class="row">
			<div class="col-md-12">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">
								<span class="not-null-star">*</span> 协议文件
							</div>
						</div>
						<input type="file" class="hidden" name="contractFile"> <input type="text" class="form-control not-empty file-show" placeholder="请选择相关文件。" readonly="readonly">
					</div>
				</div>
			</div>
			<div class="col-md-12">
				<div class="form-group">
					<div class="input-group">
						<div class="input-group-addon">
							<div class="form-item-title">附件资料</div>
						</div>
						<input type="file" class="hidden" name="file"> <input type="text" class="form-control file-show" placeholder="请选择附件资料。" readonly="readonly">
					</div>
				</div>
			</div>
		</div>
	</fieldset>

	<hr style="margin-top: 5px;">
	<div class="row">
		<div class="col-md-12 text-right">
			<div class="btn btn-sm btn-primary create-contract">确定</div>
			&nbsp;
			<div class="btn btn-sm btn-default" onclick="Suredy.Modal.closeModal();">取消</div>
		</div>
	</div>
</form>
<script type="text/javascript">
	require([ 'suredyModal', '${request.contextPath}/app/js/form-checker.js', '${request.contextPath}/app/js/file.js', 'suredyDatetimepicker', 'jqueryForm' ], function(Modal, formChecker) {
		// 文件控件控制
		$('form.form-create-agreement-contract').on('change', 'input[type="file"]', function() {
			var $this = $(this);

			var val = $this.val();

			if (val)
				val = val.substr(val.lastIndexOf('\\') + 1);

			$('input.file-show', $this.parent()).val(val);

			// 清除没有文件的组件
			$('.file-items input.file-show').each(function(i) {
				var $this = $(this);

				if (!$this.val())
					$this.parents('.item').remove();
			});

			// 增加一个待选择节点
			var template = '<div class="form-group item">\
									<div class="input-group">\
								<div class="input-group-addon">\
									<div class="form-item-title">其他文件</div>\
								</div>\
								<input type="file" name="file" class="hidden">\
								<input type="text" class="form-control file-show" placeholder="请选其他的合同相关文件" readonly="readonly">\
								<div class="btn input-group-addon file-remove">\
									<i class="icon-remove text-danger"></i>\
								</div>\
							</div>\
						</div>';

			$('.file-items').append(template);
		});

		$('.create-contract').on('click', function() {
			var form = $('form.form-create-agreement-contract');

			// 校验表单
			if (!formChecker(form)) {
				return false;
			}

			var $this = $(this);

			if ($this.hasClass('disabled'))
				return false;

			$this.addClass('disabled');

			form.ajaxSubmit({
				success : function(data, textStatus, jqXHR) {
					if (!data || !data.success) {
						var msg = (data && data.msg) || '新建补充协议同失败！';
						alert(msg);
						$this.removeClass('disabled');
						return false;
					}

					Suredy.refreshContent('${parent.id}', [ 'div.suredy-tabs' ]);

					Modal.closeModal();
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('新建补充协议失败！\n' + errorThrown);
					$this.removeClass('disabled');
					return false;
				}
			});
		});
	});
</script>
</#if>
